package com.wan.nettyproject;


import android.util.Log;

import com.google.gson.Gson;

import de.greenrobot.event.EventBus;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class MsgServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
        //ByteBuf buf= (ByteBuf) msg;
        MessageEvent messageEvent = new MessageEvent();
MessageEnty messageEnty=new Gson().fromJson((String)msg, MessageEnty.class);

        //if (msg instanceof MessageEnty) {
            messageEvent.entity = messageEnty;
            messageEvent.event = MessageEvent.Event.MESSAGE_ACCEPT;
            EventBus.getDefault().post(messageEvent);
        //}
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        Log.e("test", "exceptionCaught..." + cause.toString());
    }
}
